package com.base.myproject.client.HR.Books;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BaseObject;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Label;

/**
 * @author cxghiu4102@gmail.com 图书查询
 * 
 */
public class SearchBook extends BaseJDBCPanel {
	BaseStore<BusiObject> store;
	PagingToolBar toolBar1 = null;

	Button search = new Button("查询");// 查询按钮

	String sql = "select * from hr_book order by returnDate asc";

	public SearchBook() {
		init();
	}

	public SearchBook(String sql) {
		sql = sql.replaceAll("count.*num", "*"); // 将传过来的查询个数的SQL语句改成查询所有在查询范围内的合同
		this.sql = sql + " order by returnDate asc";
		init();
	}

	public void init() {
		initWeigetx();
		registerMainStore(store);
		search.addListener(Events.OnClick, getListener());
	}

	private void initWeigetx() {
		toolBar1 = new PagingToolBar(15);

		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(5);
		FieldSet fs = new FieldSet();
		fs.setHeading("查询图书详情");

		fs.add(createForm());
		HorizontalPanel s = new HorizontalPanel();
		s.add(search);
		fs.add(s);

		ContentPanel panel2 = new ContentPanel();
		panel2.setHeaderVisible(false);
		panel2.add(this.createGrid());
		panel2.setBottomComponent(toolBar1);

		vp.add(fs);
		vp.add(panel2);
		add(vp);
	}

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bookNo");
		type.getField("bookNo").setType(String.class);
		type.addField("sort");
		type.getField("sort").setType(String.class);
		type.addField("bookName");
		type.getField("bookName").setType(String.class);
		type.addField("library");
		type.getField("library").setType(String.class);
		type.addField("address");
		type.getField("address").setType(String.class);
		type.addField("status");
		type.getField("status").setType(String.class);
		type.addField("isbreak");
		type.getField("isbreak").setType(String.class);
		type.addField("addDate");
		type.getField("addDate").setType(Date.class);
		type.addField("returnDate");
		type.getField("returnDate").setType(Date.class);
		
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_book");
		info.setKeyname(new String[] { "icode" });
		info.setSql(sql);
		store = new BaseStore<BusiObject>(type, info);
		store.setToolBar(toolBar1);
		return store;
	}

	BaseGrid<BusiObject> grid = null;
	CheckBoxSelectionModel<BusiObject> cbs;

	BaseGrid<BusiObject> createGrid() {
		cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C2 = new ColumnConfig("bookNo", "条形码", 120);
		columns.add(C2);
		ColumnConfig C4 = new ColumnConfig("bookName", "书名", 160);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("library", "所在馆", 80);
		columns.add(C5);
		ColumnConfig C9 = new ColumnConfig("address", "藏馆地", 100);
		columns.add(C9);
		ColumnConfig C6 = new ColumnConfig("status", "状态", 80);
		columns.add(C6);
		ColumnConfig C7 = new ColumnConfig("isbreak", "是否损坏", 80);
		columns.add(C7);
		ColumnConfig C8 = new ColumnConfig("addDate", "借入日期", 100);
		C8.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C8);
		ColumnConfig C10 = new ColumnConfig("returnDate", "规定还书日期", 100);
		C10.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C10);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm, false, false);
		Tools.setResize(grid, 0.98, 0.65);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setLoadMask(true);
		grid.setBorders(false);
		grid.getStore().reload(); // 暂时先用reload，因为不reload的话，分面不出来效果，但是这样数据多了会慢。再想解决方法
		return grid;
	}

	FormPanel form;
	BaseComboBoxForm<BaseObject> bookCode = new BaseComboBoxForm<BaseObject>("bookNo", "bookName", "hr_book", "", true);
	BaseComboBoxX type = new BaseComboBoxX("name", "dictionary",
	"where type = 'hr_bookstyle'");
	// 图书类别
	DateField returnDateStart = new DateField(); // 有效开始日期
	DateField returnDateEnd = new DateField(); // 有效结束日期

	private FormPanel createForm() {
		form = new FormPanel();
		returnDateStart.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		returnDateEnd.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));

		TableLayout tl = new TableLayout(8);

		form.setWidth(913);
		form.setPadding(4);
		form.setBodyBorder(false);
		form.setHeaderVisible(false);
		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		
		form.add(new Label("图书类型:"), td);
		form.add(type, td);

		form.add(new Label("图书名称:"), td);
		form.add(bookCode, td);

		form.add(new Label("归还日期从:"), td);
		form.add(returnDateStart, td);

		form.add(new Label("到:"), td);
		form.add(returnDateEnd, td);

		form.setLayout(tl);

		return form;
	}

	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				String searchSql = "";
				if (sql.equals("select * from hr_book order by returnDate asc")) {
					searchSql = "select * from hr_book where 1=1";
				} else {
					searchSql = sql.substring(0,sql.length() - 24);
				}
				if (!bookCode.getRawValue().equals("")) {
					searchSql = searchSql + " and bookNo like '%25"
							+ bookCode.getValue().get("code") + "%25'";
				}
				if (!type.getRawValue().equals("")) {
					searchSql = searchSql + " and sort = '"
							+ type.getValue().get("code") + "'";
				}
				if (!returnDateStart.getRawValue().equals("")) {
					searchSql = searchSql + " and returnDate >= '"
							+ returnDateStart.getRawValue() + "'";
				}
				if (!returnDateEnd.getRawValue().equals("")) {
					searchSql = searchSql + " and returnDate <= '"
							+ returnDateEnd.getRawValue() + "'";
				}
				searchSql = searchSql + " order by returnDate asc";
				grid.getStore().getStoreinfo().setSql(searchSql);
				grid.getStore().reload();
			}
		};
		return defaultlistener;
	}
}
